package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private SqlSession session = null;
    public List<User> findAll() throws IOException {

        //1.读取mybatis核心配置文件的信息:运行环境,映射配置文件信息
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2.构建SQLSessionFactory对象(构建者模式)
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3.获取SqlSession对象
        session = factory.openSession();
        List<User> list = session.selectList("one.findAll");
        //5.事务提交(查询有事务用事务,没事务就拉倒)
        session.commit();
        //6.释放资源
        session.close();
        is.close();
        return list;
    }
}
